package cn.com.godliu.rabbitmqprovider.controller;

import cn.com.godliu.rabbitmqprovider.entity.UserInfo;
import cn.com.godliu.rabbitmqprovider.response.CommonReponse;
import cn.com.godliu.rabbitmqprovider.service.UserService;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    UserService userService;

    @PostMapping(value = "/addUser", produces = {"application/json;charset=UTF-8"})
    public String addUser(@RequestBody UserInfo userInfo) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("添加成功");
            commonReponse.setObj(userService.addUser(userInfo));
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("添加失败");
        }
        return new Gson().toJson(commonReponse);
    }

    @PostMapping("/removeUse")
    public String removeUser(@RequestBody UserInfo userInfo) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("删除成功");
            userService.removeUser(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("删除失败");
        }
        return new Gson().toJson(commonReponse);
    }

    @PostMapping("/updateUse")
    public String updateUse(@RequestBody UserInfo userInfo) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("更新成功");
            commonReponse.setObj(userService.updateUser(userInfo));
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("更新失败");
        }
        return new Gson().toJson(commonReponse);
    }

    @GetMapping("/getUser")
    public String getUser(int id) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("查询成功");
            commonReponse.setObj(userService.getUserById(id));
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("查询失败");
        }
        return new Gson().toJson(commonReponse);
    }

    @GetMapping("/getUsers")
    public String getUsersOfPage(int pageNum, int pageSize) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("查询成功");
            List<UserInfo> list = userService.getUsersByPage(pageNum, pageSize);
            commonReponse.setObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("查询失败");
        }
        return new Gson().toJson(commonReponse);
    }

    @GetMapping("/getUsersOfPageinfo")
    public String getUsersOfPageinfo(int pageNum, int pageSize) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("查询成功");
            PageInfo<UserInfo> list = userService.getUsersofPageInfoByPage(pageNum, pageSize);
            commonReponse.setObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("查询失败");
        }
        return new Gson().toJson(commonReponse);
    }

    @GetMapping("/getUsersAbountAge")
    public String getUsersOfPageinfo(int age, int pageNum, int pageSize) {
        CommonReponse commonReponse = new CommonReponse();
        try {
            commonReponse.setCode(1);
            commonReponse.setMsg("查询成功");
            List<UserInfo> list = userService.getUsersByPage(age, pageNum, pageSize);
            commonReponse.setObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            commonReponse.setCode(0);
            commonReponse.setMsg("查询失败");
        }
        return new Gson().toJson(commonReponse);
    }
}
